    import Crypto from '@/common/js/oss/crypto.js';
    import '@/common/js/oss/hmac.js';
    import '@/common/js/oss/sha1.js';
    import {
        Base64
    } from '@/common/js/oss/base64.js';

    const uploadFileSize = 1024 * 1024 * 2; // 上传文件的大小限制100m
    const uploadVideoSize = 1024 * 1024 * 50; // 上传文件的大小限制100m
	const accesskey = 'P8husyKkH6vHBi4OQLkPOOQUEdSFyJ'; // 自己去申请
	const accessId = 'LTAI4GJRNDdBXWWSErj2QD6N' // 自己去申请
	var type = 0;

    export default {
        _getPolicy() {
			var uploadSize = (type==0?uploadFileSize:uploadVideoSize);
            let policyText = {
                "expiration": "2032-10-09T12:00:00.000Z", //设置该Policy的失效时间，超过这个失效时间之后，就没有办法通过这个policy上传文件了
                "conditions": [
                    ["content-length-range", 0, uploadSize] // 设置上传文件的大小限制
                ]
            };

            return Base64.encode(JSON.stringify(policyText))
        },
        _getSignature(message) {
            let bytes = Crypto.HMAC(Crypto.SHA1, message, accesskey, {
                asBytes: true
            });
            return Crypto.util.bytesToBase64(bytes);
        },
        _getSuffix(filename) {
            let pos = filename.lastIndexOf('.')
            let suffix = ''
            if (pos != -1) {
                suffix = filename.substring(pos)
            }
            return suffix;
        },
        getFileName(filename) {
            return 'user/' + new Date().getTime() + Math.random().toString(36).substring(3, 20) + this._getSuffix(filename)
        },
        getImage() {
			type = 0;
            return new Promise((resolve, reject) => {
                uni.chooseImage({
                    count: 1, //默认9
                    sizeType: ['compressed'], //可以指定是原图还是压缩图，默认二者都有
                    sourceType: ['album', 'camera'],
                    success: (res) => {
                        resolve(res)
                    }
                })
            });
        },
        getVideo() {
			type = 1;
            return new Promise((resolve, reject) => {
                uni.chooseVideo({
                    count: 1,
                    sourceType: ['camera', 'album'],
                    success: function(res) {
                        if (res.size > uploadVideoSize) {
                            uni.showToast({
                                title: '文件大小超过系统上传限制：' + uploadVideoSize,
                                icon: 'none',
                                duration: 1000
                            });
                            return;
                        }
                        resolve(res.tempFilePath)
                    },
                    fail: () => {
						console.error('取消选择视频');
                    }
                })
            });
        },
        // 获取STS签名
        get_STS() {
            // 读取接口
            /* return new Promise((resolve, reject) => {
                let name = new Date().getTime() + Math.random();
                let operate = 'uploadImg';
                let that = this;
                uni.request({
                    method: "GET",
                    url: "*************",
                    data: {
                        name,
                        operate,
                        sign: Crypto.MD5(name + operate + "bayinabayin888").toString()
                    },
                    success(res) {
                        if (res.data.code != 200) {
                            that.$alert(res.data.message || res.data.msg)
                        } else {
                            resolve(res.data.data)
                        }
                    },
                    error(err) {
                        reject(err)
                    }
                })
            }) */
            // 本地获取
            return new Promise((resolve, reject) => {
                let policy = this._getPolicy();
                let res = {
                    accessId: accessId,
                    host: 'https://static.dhlshu.cn',  // 自己去申请
                    policy,
                    signature: this._getSignature(policy),
                };
                resolve(res)
            })
        },
		async upload(imageSrc){
			var self = this;
			let sign = await this.get_STS();
			uni.showLoading({
				title: '上传中'
			});
			let fileName = this.getFileName(imageSrc);
			return new Promise((resolve, reject) => {
				uni.uploadFile({
					url: sign.host,
					filePath: imageSrc,
					fileType: 'image',
					name: 'file',
					formData: {
						name: imageSrc,
						key: fileName,
						policy: sign.policy,
						OSSAccessKeyId: sign.accessId,
						success_action_status: '200',
						signature: sign.signature,
					},
					success: (res) => {
						uni.hideLoading();
						if(res.statusCode==200){
							resolve(sign.host + '/' + fileName);
						}else{
							reject(res);
						};
					},
					fail: (err) => {
						uni.hideLoading();
						reject(err);
					}
				})
			})
		}
    };